import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

public class DBOpertatorUI extends JFrame {
    private JTextField ipField;
    private JTextField portField;
    private JTextField userField;
    private JPasswordField passwordField;
    private JButton testConnBtn;
    private JButton okBtn;
    private JButton cancelBtn;

    public DBOpertatorUI() {
        setTitle("MySQL - 创建连接");
        setSize(300,300);
        createComponents();
        addComponents();
    }

    private void createComponents(){
        ipField = new JTextField("localhost",10);
        portField = new JTextField("3306",10);
        userField = new JTextField("root",10);
        passwordField = new JPasswordField(10);

        testConnBtn = new JButton("连接测试");
        testConnBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("正在测试中");
                String ip = ipField.getText();
                String port = portField.getText();
                String user = userField.getText();
                String password = new String(passwordField.getPassword());

                DBOpertator dbOpertator = new DBOpertator();
                try {
                    if (dbOpertator.getConnection(ip,port,user,password) != null){
                        JOptionPane.showMessageDialog(null, "连接成功!");
                    }
                } catch (SQLException ex) {
                    JOptionPane.showMessageDialog( null,"连接失败!"+ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
                    throw new RuntimeException(ex);
                }
            }
        });

        okBtn = new JButton("确定");
        cancelBtn = new JButton("取消");

    }
    private void addComponents(){
        setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));

        JPanel row1 = new JPanel(new FlowLayout());
        row1.add(new JLabel("主机IP："));
        row1.add(ipField);

        JPanel row2 = new JPanel(new FlowLayout());
        row2.add(new JLabel("端口号："));
        row2.add(portField);

        JPanel row3 = new JPanel(new FlowLayout());
        row3.add(new JLabel("用户名："));
        row3.add(userField);

        JPanel row4 = new JPanel(new FlowLayout());
        row4.add(new JLabel("密  码："));
        row4.add(passwordField);

        JPanel row5 = new JPanel(new FlowLayout());
        row5.add(testConnBtn);
        row5.add(Box.createHorizontalStrut(50));
        row5.add(okBtn);
        row5.add(cancelBtn);

        add(row1);
        add(row2);
        add(row3);
        add(row4);
        add(Box.createVerticalStrut(50));
        add(row5);

    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                DBOpertatorUI dbui = new DBOpertatorUI();
                dbui.setVisible(true);
            }
        });
    }
}
